class StudentEvaluateController {
    def authenticateService

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        def studentEvaluateInstanceList = StudentEvaluate.findAllByStudent(Student.get(params.id))
        [studentEvaluateInstanceList: studentEvaluateInstanceList, studentEvaluateInstanceTotal: studentEvaluateInstanceList.size()]
    }

    def create = {
        def studentEvaluateInstance = new StudentEvaluate()
        studentEvaluateInstance.properties = params
        return [studentEvaluateInstance: studentEvaluateInstance,studentID:params.id]
    }

    def save = {
        def studentEvaluateInstance = new StudentEvaluate(params)
        studentEvaluateInstance.student = Student.get(params.studentID);
        studentEvaluateInstance.postTeacher = authenticateService.userDomain()
        def flag = params.evaluateFlag?params.evaluateFlag:''

        if (studentEvaluateInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), studentEvaluateInstance])}"
            redirect(action: "list",id:studentEvaluateInstance.student.id,params:[evaluateFlag:flag])
        }
        else {
            render(view: "create", model: [studentEvaluateInstance: studentEvaluateInstance,evaluateFlag:flag])
        }
    }

    def show = {
        def flag = params.evaluateFlag?params.evaluateFlag:''

        def studentEvaluateInstance = StudentEvaluate.get(params.id)
        if (!studentEvaluateInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), params.id])}"
            redirect(action: "list",params:[evaluateFlag:flag])
        }
        else {
            [studentEvaluateInstance: studentEvaluateInstance,studentID:studentEvaluateInstance.postTeacher.id]
        }
    }

    def edit = {
        def flag = params.evaluateFlag?params.evaluateFlag:''
        def studentEvaluateInstance = StudentEvaluate.get(params.id)
        if (!studentEvaluateInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), params.id])}"
            redirect(action: "list",params:[evaluateFlag:flag])
        }
        else {
            return [studentEvaluateInstance: studentEvaluateInstance,studentID:studentEvaluateInstance.student.id]
        }
    }

    def update = {
        def studentEvaluateInstance = StudentEvaluate.get(params.id)
        def flag = params.evaluateFlag?params.evaluateFlag:''
        if (studentEvaluateInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (studentEvaluateInstance.version > version) {
                    
                    studentEvaluateInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'studentEvaluate.label', default: 'StudentEvaluate')] as Object[], "Another user has updated this StudentEvaluate while you were editing")
                    render(view: "edit", model: [studentEvaluateInstance: studentEvaluateInstance,evaluateFlag:flag])
                    return
                }
            }
            studentEvaluateInstance.properties = params
            if (!studentEvaluateInstance.hasErrors() && studentEvaluateInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), studentEvaluateInstance])}"
                redirect(action: "list",id:studentEvaluateInstance.student.id,params:[evaluateFlag:flag])
            }
            else {
                render(view: "edit", model: [studentEvaluateInstance: studentEvaluateInstance,evaluateFlag:flag])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), params.id])}"
            redirect(action: "list",params:[evaluateFlag:flag])
        }
    }

    def delete = {
        def flag = params.evaluateFlag?params.evaluateFlag:''
        def studentEvaluateInstance = StudentEvaluate.get(params.id)
        if (studentEvaluateInstance) {
            try {
                studentEvaluateInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), params.id])}"
                redirect(action: "list", id: params.studentID,params:[evaluateFlag:flag])
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), params.id])}"
                redirect(action: "show", id: params.id,params:[studentID:params.studentID,evaluateFlag:flag])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'studentEvaluate.label', default: 'StudentEvaluate'), params.id])}"
            redirect(action: "list", id: params.id,params:[studentID:params.studentID,evaluateFlag:flag])
        }
    }
}
